package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.IHomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * springCache缓存同步任务
 */
@Slf4j
@Component
public class SpringCacheSyncHandler {
    @Resource
    private IRegionService regionService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IHomeService homeService;

    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheSync() {
        //1、组装key，使用RedisTemplate删除
        String key = RedisConstants.CacheName.JZ_CACHE+"::ACTIVE_REGIONS";
        redisTemplate.delete(key);

        //2、查询所有已启用的区域集合，会利用SpringCache将数据写入缓存
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();

        //3、缓存每个区域的首页服务列表
        regionSimpleResDTOS.forEach(rs->{
            //3.1 组装不同区域的首页服务列表数据的key
            String home_key = RedisConstants.CacheName.SERVE_ICON+"::"+rs.getId();
            String type_key = RedisConstants.CacheName.SERVE_TYPE+"::"+rs.getId();
            String hot_key = RedisConstants.CacheName.HOT_SERVE+"::"+rs.getId();
            redisTemplate.delete(home_key);
            redisTemplate.delete(type_key);
            redisTemplate.delete(hot_key);


            //3.2 执行缓存
            homeService.queryServeIconCategoryByRegionIdCache2(rs.getId());
            homeService.queryServeIconCategoryByRegionIdCache3(rs.getId());
            homeService.queryServeIconCategoryByRegionIdCache4(rs.getId());
        });



    }






}
